package com.houseman.demo;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.houseman.workFlow.utils.DBUtils;

@SuppressWarnings("serial")
public class LoginAction extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String id = req.getParameter("userId");
		String name = req.getParameter("userName");
		
		//获得连接
		try {
			Connection conn = DBUtils.geConnection();
			PreparedStatement ps = conn.prepareStatement("SELECT COUNT(*) FROM USER WHERE USERID=? AND USERNAME=?");
			ps.setString(1, id);
			ps.setString(2, name);
			ResultSet rs = ps.executeQuery();
			
			int count = 0;
			while(rs.next()) {
				count = rs.getInt(1);
			}
			if(count == 1) {
				req.getSession().setAttribute("userId", id);
				req.getSession().setAttribute("userName", name);
				resp.sendRedirect("/workFlow/myPage.action");
			}else {
				req.getRequestDispatcher("/login.jsp").forward(req, resp);
			}
			
			DBUtils.close(conn, ps, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
}
